<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>简聊</title>
</head>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>

<script>
    $(function(){
        $("#div").hide();
        $(".on-line").hide();
    })
    var socket;
    function openSocket() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else{
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            //等同于socket = new WebSocket("ws://localhost:8888/xxxx/im/25");
            //var socketUrl="${request.contextPath}/im/"+$("#userId").val();
            var socketUrl="http://localhost:7522/websocket/"+$("#userId").val();
            //var socketUrl="http://localhost:8036/websocketTest/"+$("#userId").val();
            socketUrl=socketUrl.replace("https","ws").replace("http","ws");
            console.log(socketUrl);
            if(socket!=null){
                socket.close();
                socket=null;
            }
            socket = new WebSocket(socketUrl);
            //打开事件
            socket.onopen = function() {
                console.log("websocket已打开");
                $("#div").show();
                $(".on-line").show();
                $("#login").hide();
                //socket.send("这是来自客户端的消息" + location.href + new Date());
            };
            //获得消息事件
            socket.onmessage = function(msg) {
                console.log(msg);
                if(msg.data == "连接成功"){
                    $("#sendMessage").css("color","green");
                }else{
                    var user = JSON.parse(msg.data);
                    console.log(user);
                    $("#user").text("【"+user.fromUserId+"】的消息：");
                    $("#msg").empty();
                    $("#msg").text(user.contentText);
                }

                console.log(msg.data);
                //发现消息进入    开始处理前端触发逻辑
            };
            //关闭事件
            socket.onclose = function() {
                console.log("websocket已关闭");
            };
            //发生了错误事件
            socket.onerror = function(e) {
                console.log(e);
                console.log("websocket发生了错误");
            }
        }
    }
    function sendMessage() {
        var msg = $("#contentText").val();
        $("#contentText").val("");
        console.log(msg);
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
            $("#msg").val("您的浏览器不支持WebSocket");
        }else {
            console.log("您的浏览器支持WebSocket");
            console.log('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+msg+'"}');
            socket.send('{"toUserId":"'+$("#toUserId").val()+'","contentText":"'+msg+'"}');
        }
    }
    //送礼物
    function givingGifts(){
        $.ajax({
            url:'/gift/randomGift',
            post:'get',
            data:{"fromUserId":$("#userId").val(),"toUserId":$("#toUserId").val()},
            success:function(data){
                console.log();
            },error:function(e){
                console.log(e);
            }
        })
    }
</script>
<body style="margin: 0;width: 100%;margin: auto">
<div style="margin: 10% 50% 10% 25%;border: 1px solid #cefff4;padding:1% 2% 1% 2%;background-color: #cefff4">
    <div>
        <p><h1>无痕聊天室😳</h1></p>
        <p style="padding-left: 70%" class="on-line">
            <span style="color: purple">在线</span>|
            <span style="color: crimson;cursor: pointer" onclick="givingGifts()">送礼物</span>
        </p>
        <p><span id="gift"></span></p>
    </div>
    <div>
        <p>【😀】:&emsp;
            <select id="userId">
                <option value="S">S</option>
                <option value="N">N</option>
            </select></p>
        <p>【😄】:&emsp;
            <select id="toUserId">
                <option value="N">N</option>
                <option value="S">S</option>
            </select>
        </p>
        <p><span id="user"></span><span id="msg"></span></p>
        <p>&nbsp;
            <input id="contentText" name="contentText">
            <!--<textarea id="contentText" name="contentText" type="text" style="width: 100%; height: 200px;">
            </textarea>-->
        </p>
        <p><div id="div" style="margin-left: 70%"><button id="sendMessage" onclick="sendMessage()" style="cursor: pointer">发送消息</button></div>
        <p style="padding-left: 70%"><button id="login" onclick="openSocket()" style="cursor: pointer">上线</button></p>
    </div>
</div>
</body>

</html>